/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sensorgui;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 *
 * @author Conor
 */
public class Sensor{
	
	protected PropertyChangeSupport pcs;

    private int xPos;
    private int yPos;
    private int width;
    private int height;

    private double range = 60;
    private Direction direction = new Direction(SensorConstants.north);
    private double defaultDir = SensorConstants.north;
    
    private int span = 0;
    private Grid myGrid;

    public Sensor(int x, int y, int span ,double defaultDir, Grid myGrid){
        xPos = x;
        yPos = y;
        this.defaultDir = defaultDir;
        
        direction = new Direction(this.defaultDir);
        this.span = span;
        this.myGrid = myGrid;
        
        pcs = new PropertyChangeSupport(this);
    }
    
    public boolean viewObject(){
    	boolean objectViewed = false;
    	
    	
    	return objectViewed;
    }

    public void moveRight(){
    	
    	double currentDir = direction.getValue();
    	if (this.canMoveRight()) {
    		direction.setValue(currentDir+5);
    		pcs.firePropertyChange("direction", currentDir, direction.getValue());
    	}
    	
    }
    
    public boolean canMoveRight(){
    	boolean canMove = true;
    	
    	double currentDir = direction.getValue();
    	Direction tempDir = new Direction(defaultDir+span);
    	
    	if (currentDir+5 == tempDir.getValue()) {
    		canMove = false;
    	}
    	
    	return canMove;
    }
    
    public void moveLeft(){
    	
    	double currentDir = direction.getValue();
    	if (this.canMoveLeft()) {
    		direction.setValue(currentDir-5);
    		pcs.firePropertyChange("direction", currentDir, direction.getValue());
    	} 
    	
    }
    
    public boolean canMoveLeft(){
    	boolean canMove = true;
    	
    	double currentDir = direction.getValue();
    	Direction tempDir = new Direction(defaultDir-span);
    	
    	if (currentDir-5 == tempDir.getValue()) {
    		canMove = false;
    	}
    	
    	return canMove;
    }
    
    public void setDirection(final Direction direction){
        Direction oldDir = this.direction;
    	this.direction = direction;
        pcs.firePropertyChange("direction", oldDir, direction);
    }

    public Direction getDirection(){
        return direction;
    }

    public void setX(int xPos){
        this.xPos = xPos;
    }

    public int getX(){
        return xPos;
    }

    public void setY(int yPos){
        this.yPos = yPos;
    }

    public int getY(){
        return yPos;
    }

    public int getWidth(){
        return width;
    }

    public void setWidth(int width){
        this.width = width;
    }

    public int getHeight(){
        return height;
    }

    public void setHeight(int height){
        this.height = height;
    }

    public double getRange(){
        return range;
    }
    
    //Delegate listener registration and event firing to the PropertyChangeSupport object
	
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		pcs.addPropertyChangeListener(listener);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		pcs.removePropertyChangeListener(listener);
	}
	

}
